<?php

    class LeproUser {
    
        public function __construct($id, $username) {
            $this->_id = $id;
            $this->_username = $username;
            //$this->_rating = $rating;
            //$this->_karma = $karma;
            $this->_cardId = null;
            $this->_groupsIds = array();
            $this->_pwdHash = null;
            $this->_email = null;
            $this->_gScore = 0;
            $this->_rTime = null;
        }
        
        public function getID() {
            return $this->_id;
        }
        
        public function getUsername() {
            return $this->_username;
        }
        
        public function getCardID() {
            return $this->_cardId;
        }
        
        public function setCardID($cardId) {
            $this->_cardId = $cardId;
        }
        
        public function getPwdHash() {
            return $this->_pwdHash;
        }
        
        public function setPwdHash($pwdHash) {
            $this->_pwdHash = $pwdHash;
        }        
        
        public function getEmail() {
            return $this->_email;
        }
        
        public function setEmail($email) {
            $this->_email = $email;
        }        
        
        public function tieWithGroup($group) {
            $groupId = $group->getID();
            if (!in_array($groupId, $this->_groupsIds)) {
                $this->_groupsIds[] = $groupId;                
                /* if ($newUserScore != null) {
                    $this->_gScore = $newUserScore; 
                } else { 
                    $this->addGroupScore($group->getValue());
                } */
            }
        }
        
        public function leaveGroup($group) {
            $groupId = $group->getID();
            $key = array_search($groupId, $this->_groupsIds);
            if ($key === false) return;
            unset($this->_groupsIds[$key]);
            /* if ($newUserScore != null) {
                $this->_gScore = $newUserScore; 
            } else { 
                $this->subtractGroupScore($group->getValue());
            } */
        }
        
        public function inGroup($groupId) {
            return (array_search($groupId, $this->_groupsIds) !== false);
        }
        
        public function assignGroups($groupsIds) {
            $this->_groupsIds = $groupsIds;
        }
        
        public function getGroupsIDs() {
            // return array_values($this->_groupsIds);
            return $this->_groupsIds;
        }        
        
        public function getGroupsCount() {
            return count($this->_groupsIds);
        }
        
        public function getGScore() {
            return $this->_gScore;
        }
        
        public function setGScore($gScore) { // for DAO
            $this->_gScore = $gScore;
        }
        
        public function updateGScore($gScore) { // for controller
            $this->_gScore = $gScore;
        }
        
        public function getR10nTime() {
            return $this->_rTime;
        }
        
        public function setR10nTime($rTime) { // for DAO
            $this->_rTime = $rTime;
        }
        
        public function setTimeRegistered($rTime) { // for controller
            $this->_rTime = $rTime;
        }        
        
        protected function addGroupScore($gScore) {
            $this->_gScore += $gScore;
        } 
        
        protected function subtractGroupScore($gScore) {
            $this->_gScore -= $gScore;
        }
        
        /*
        public function setRating($rating) {
            $this->_rating = $rating;
        }
        
        public function setKarma($karma) {
            $this->_karma = $karma;
        } */       
        
        public static function getVoice($rating) {
            /* RATE / VOICE: */
            /*          x <     0: 1 */
            /* 64    <= x <   255: 2 */
            /* 256   <= x <  1023: 3 */
            /* 1024  <= x <  4095: 4 */
            /* 4096  <= x < 16383: 5 */
            /* 16384 <= x < 65535: 6 */
            /* 65536 <= x        : 7 */
            // TODO: optimize using binary operators
            if ($rating > 65535) return 7;
            if ($rating > 16383) return 6;
            if ($rating > 4095)  return 5;
            if ($rating > 1023)  return 4;
            if ($rating > 255)   return 3;
            if ($rating > 63)    return 2;
            return 1;
        }
        
        public function getSummary() {
            return array(
                    "uname" => $this->getUsername(),
                    "uid" => $this->getID(),
                    "card" => $this->getCardID(),
                    "gscore" => $this->getGScore(),
                    "groups" => $this->getGroupsIDs(),
                    "grcount" => $this->getGroupsCount(),
                    "rtime" => $this->getR10nTime()
                );
        }
        
        /* public function getRating() {
            return $this->_rating;
        }
        
        public function getKarma() {
            return $this->_karma;
        } */
        
        /* public function getGroupsIDs() {
            return array_keys($this->_groups);
        } */
    
    } 

?>
